// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef __PLANETARYPAGEHEIGHTMAP_H_
#define __PLANETARYPAGEHEIGHTMAP_H_

#include "planetaryscenemanager.h"
#include "planetaryoptions.h"

namespace Ogre
{
	/**
	* Small container class for a page heightmap.
	* This class inlcudes the heighmap data and some functions to make reading of it easier.
	*/
	class PlanetaryPageHeightmap
	{
	public:
		PlanetaryPageHeightmap(PlanetarySceneManager *mgr,float *data, unsigned int size)
		{
			mSceneMgr = mgr;
			mOptions = mgr->getOptions();

			mData = data;
			mDataSize = size;
		}

		~PlanetaryPageHeightmap()
		{
			delete[] mData;
		}

		/// Returns a pointer to the first value in a row inside a tile. Since a page is split into multiple tiles and heightmaps contains data for an entire page.
		/// @param tileX The X index of the tile to retrieve.
		/// @param tileZ The Z index of the tile to retrieve.
		/// @param row The index of the row inside the tile to return.
		inline const float *getTileDataRow(unsigned int tileX, unsigned int tileZ,unsigned int row) const
		{
			const unsigned int rowlength = ((mOptions->PageSize * (mOptions->TileSize - 1)) + 1);
			const unsigned int addition = (tileZ * rowlength * (mOptions->TileSize - 1)) + // Tile row
				(tileX * (mOptions->TileSize - 1)) + // Tile Column
				rowlength * row; // Internal tile row

			assert(addition < mDataSize);

			return mData + addition;
		}

		/// Returns a pointer to a row of the page heightmap
		/// @param row The index of the row to retun.
		/// @param LOD As the heightmap size is different for different LOD levels, this info is needed to calculate the correct row position.
		inline const float *getRow(unsigned int row,unsigned int LOD = 0) const
		{
			unsigned int rowlength;
			switch(LOD)
			{
			case 0:
				rowlength = ((mOptions->PageSize * (mOptions->TileSize - 1)) + 1);
				break;
			case 1:
				rowlength = mOptions->mPageSizeLOD1;
				break;

			default:
				return NULL;
			}

			return mData + rowlength * row;
		}

		/// Actual heightmap data
		float *mData;

		/// Data size
		unsigned int mDataSize;

	private:
		/// Scene Manager object
		PlanetarySceneManager *mSceneMgr;

		/// Options object
		PlanetaryOptions *mOptions;
	};
}

#endif
